home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / Storage / Bento / CM / Utility.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  6.1 KB  |  178 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Utility.c
  3.  
  4.     Contains:    Container Manager Miscellaneous Utility Routines
  5.  
  6.     Written by:    Ira L. Ruben
  7.  
  8.     Owned by:    Ed Lai
  9.  
  10.     Copyright:    © 1991-1994 by Apple Computer, Inc., all rights reserved.
  11.  
  12.     Change History (most recent first):
  13.  
  14.          <2>     8/26/94    EL        #1181622 Ownership update.
  15.          <1>      2/3/94    EL        first checked in
  16.  
  17.     To Do:
  18. */
  19.  
  20. /*---------------------------------------------------------------------------*
  21.  |                                                                           |
  22.  |                         <<<     Utility.c     >>>                         |
  23.  |                                                                           |
  24.  |             Container Manager Miscellaneous Utility Routines              |
  25.  |                                                                           |
  26.  |                               Ira L. Ruben                                |
  27.  |                                 12/02/91                                  |
  28.  |                                                                           |
  29.  |                  Copyright Apple Computer, Inc. 1991-1994                 |
  30.  |                           All rights reserved.                            |
  31.  |                                                                           |
  32.  *---------------------------------------------------------------------------*
  33.  
  34.  This file containes a collection of generally usefull utility routines that don't
  35.  logically fit anywhere else.
  36. */
  37.  
  38.  
  39. #include <stddef.h>
  40. #include <string.h>
  41. #include <stdio.h>
  42.  
  43. #ifndef __CMTYPES__
  44. #include "CMTypes.h"
  45. #endif
  46. #ifndef __CM_API_TYPES__
  47. #include "CMAPITyp.h"
  48. #endif
  49. #ifndef __TOCENTRIES__
  50. #include "TOCEnts.h"   
  51. #endif
  52. #ifndef __TOCOBJECTS__
  53. #include "TOCObjs.h"   
  54. #endif
  55. #ifndef __CONTAINEROPS__
  56. #include "Containr.h"  
  57. #endif
  58. #ifndef __UTILITYROUTINES__
  59. #include "Utility.h"        
  60. #endif
  61. #ifndef __HANDLERS__
  62. #include "Handlers.h"
  63. #endif
  64. #ifndef __SESSIONDATA__
  65. #include "Session.h"          
  66. #endif
  67.  
  68.                                                                     CM_CFUNCTIONS
  69.  
  70. /* The following generates a segment directive for Mac only due to 32K Jump Table             */
  71. /* Limitations.  If you don't know what I'm talking about don't worry about it.  The        */
  72. /* default is not to generate the pragma.  Theoritically unknown pragmas in ANSI won't    */
  73. /* choke compilers that don't recognize them.  Besides why would they be looked at if        */
  74. /* it's being conditionally skipped over anyway?  I know, famous last words!                        */
  75.  
  76. #if CM_MPW
  77. #pragma segment UtilityRoutines
  78. #endif
  79.  
  80.  
  81. /*-----------------------------------------------*
  82.  | cmltostr - convert a long integer to a string |
  83.  *-----------------------------------------------*
  84.  
  85.  Converts the (long) integer n to a string. If width>0 and width > strlen(string), then
  86.  the number will be right-justified in a string of width characters.  If width<0 and 
  87.  abs(width) > length(string), then the number is again right-justified but padded with
  88.  leading zeros instead of blanks.    In this case a negative number will have a minus as
  89.  its first character.  If width is too small the entire number is returned overflowing
  90.  the width.
  91.  
  92.  If hex conversion is desired, set the hexConversion parameter to true.
  93.  
  94.  The function returns the specified string parameter as its value. That string is assumed
  95.  large enough to hold the number.
  96.  
  97.  Note, this routine is provided as a substitute for sprintf().  It is more compact, faster,
  98.  and doesn't "drag" in a shit-load of I/O library routines with it.
  99. */
  100.  
  101. CM_CHAR *cmltostr(CM_LONG n, CM_SHORT width, CMBoolean hexConversion, CM_CHAR *s)
  102. {
  103.     CMBoolean     leading0s, neg = false;
  104.     CM_SHORT         i, len, padding, w;
  105.     CM_CHAR          filler, tmpStr[30], *d, *t, c1, c2, *s0 = s;
  106.     
  107.     /* Convert the number to a string with NO leading zeros.  A zero value is just the        */
  108.     /* string "0". After we have the string we "format" it.    Note, when we get done with        */
  109.     /* this conversion, t will point to the first digit of the string.                                        */
  110.     
  111.     if (n == 0)                                                            /* handle 0 specially                                                    */
  112.         strcpy(t = tmpStr, "0");
  113.     else if (hexConversion) {                                /* converting to hex...                                                */
  114.         for (t = tmpStr, d = (CM_CHAR *)&n, leading0s = true, i = sizeof(long); i; i--, d++) {
  115.             c1 = "0123456789ABCDEF"[(*d >> 4) & 0x0F];
  116.             c2 = "0123456789ABCDEF"[*d & 0x0F];
  117.             if (leading0s) {
  118.                 if (c1 != '0') {
  119.                     *t++ = c1;
  120.                     leading0s = false;
  121.                 }
  122.             } else
  123.                 *t++ = c1;
  124.             if (leading0s) {
  125.                 if (c2 != '0') {
  126.                     *t++ = c2;
  127.                     leading0s = false;
  128.                 }
  129.             } else
  130.                 *t++ = c2;
  131.         } /* for */
  132.         *t = '\0';
  133.         t = tmpStr;
  134.     } else {                                                                    /* converting to decimal...                                    */
  135.         if (n < 0) {
  136.             neg = true;
  137.             n = -n;
  138.         }
  139.         t = tmpStr + 15;                                                /* the digits go in from low to high                */
  140.         *t-- = 0;
  141.         do {
  142.             *t-- = "0123456789"[n % 10];                    /* done in a "portable" way!                                */
  143.             n /= 10;
  144.         } while (n);
  145.         if (neg) 
  146.             *t = '-';
  147.         else
  148.             ++t;
  149.     }
  150.     
  151.     /* Pad the string according to the width parameter.  If the width is too small, the        */
  152.     /* entire number is returned overflowing the width.  If width large enough, pad with    */
  153.     /* 0's or blanks according to whether width is negative or positive respectively.            */
  154.     
  155.     len = (short)strlen(t);                                        /* len is converted string width width            */
  156.     w = (short)((width < 0) ? -width : width);/* w is abs(width)                                                    */
  157.     
  158.     if (len < w) {                                                        /* converted string fits in width...                */
  159.         padding = (short)(w - len);                            /* ...this is how many pad chars we need        */
  160.         if (width > 0)                                                    /* ...if width > 0...                                                */
  161.             filler = ' ';                                                    /* ...the padding is blanks                                    */
  162.         else {                                                                    /* ...if width < 0...                                                */
  163.             filler = '0';                                                    /* ...the padding is 0's, but...                        */
  164.             if (neg) {                                                        /* ...if decimal conversion and negative...    */ 
  165.                 --padding;                                                    /* ...reduce amount of padding to put in "-"*/
  166.                 *t = '0';                                                        /* ...clobber the "-" already in string            */
  167.                 *s++ = '-';                                                    /* ...1st output char is the sign                        */
  168.             }
  169.         }
  170.         while (padding--) *s++ = filler;                /* ...put in the padding                                        */
  171.     }
  172.     
  173.     strcpy(s, t);                                                            /* copy the digits to the output                        */
  174.     return (s0);
  175. }
  176.                                                          
  177.                                                           CM_END_CFUNCTIONS
  178.